<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>

<style type="text/css">
/* table sorter */
table.tablesorter {
	width: 100%;
	text-align: left;
}
table.tablesorter thead tr th, table.tablesorter tfoot tr th {
	background-color: #e6EEEE;
}
table.tablesorter thead tr .tblsort-header {
	cursor: pointer;
	background: url(/images/tablesorter/bg.gif) no-repeat right center;
}
table.tablesorter tbody td {
	color: #3D3D3D;
	background-color: #FFF;
	vertical-align: top;
}
table.tablesorter tbody tr.odd td {
	background-color:#F0F0F6;
}
table.tablesorter thead tr .headerSortUp {
	background: url(/images/tablesorter/asc.gif) no-repeat right center;
}
table.tablesorter thead tr .headerSortDown {
	background: url(/images/tablesorter/desc.gif) no-repeat right center;
}
table.tablesorter thead tr .headerSortDown, table.tablesorter thead tr .headerSortUp {
background-color: #8dbdd8;
}
/* end table sorter */
</style>
<link rel="stylesheet" href="<?php echo base_url()?>skins/default/jquery.cluetip.css" type="text/css" />
<script src="<?php echo base_url()?>js/jquery.cluetip.js" type="text/javascript"></script>
<div class="middle padtop10 myclear">
	<div class="page-content">
		<h3>
			<a style="color: #ff6600;" href="<?php echo site_url('zone/dashboard'); ?>">
				Zone report
			</a>
			>>
			<span>
			Danh sách zone đang chạy
			</span>
		</h3>
		
		<hr class="martop10 marbottom10 myclear" />
		<p class="padbottom5">
			<b>Quick select website theo mức giá và chuyện mục của Admarket</b>
		</p>
		<p class="padtop5 padbottom10">
			<select id="slcQSiteFilter" onchange="quickSiteSelect(this);">
				<option value="all">--------------------------</option>
				<?php
				foreach($lstQuickSiteFilter as $item)
				{
				?>
					<option value="<?php echo $item['val']; ?>"><?php echo $item['name']; ?></option>
				<?php
					foreach($item['lstCat'] as $catSite)
					{
					?>
						<option value="<?php echo $catSite['val']; ?>"><?php echo '--------' . $catSite['name']; ?></option>
					<?php
					}
				}
				?>
			</select>
		</p>
		<div class="myclear">
			<p class="padbottom5 float-left" style="width: 470px;margin-right: 40px;">
				<b >Theo website</b>
				<input type="hidden" id="hddFlterSite" value="<?php echo $strSiteId; ?>" />
			</p>
			<p class="padbottom5 float-left" style="width: 470px;">
				Bạn đang xem: <b class="font18" id="viewNumSite"><?php echo (empty($arrSiteId)) ? count($lstSite) . '/'. count($lstSite) : count($arrSiteId) . '/'. count($lstSite); ?></b> website
			</p>
		</div>
		<div class="myclear">
			<div id="divFilterSite" style="height: 300px; overflow: auto; width: 450px; float: left; margin-right: 40px; background: #fff; padding: 10px;">
				<p>
					<input type="checkbox" id="chkAllFilterSize" class="ver-middle" onclick="filterSizeAll(this);" /> <span class="ver-middle marleft5">Tất cả</span>
				</p>
				<?php
				foreach($lstSite as $site)
				{
				?>
					<p class="padtop5">
						<input type="checkbox" id="chkSite_<?php echo $site['siteid']; ?>" value="<?php echo $site['siteid']; ?>" class="ver-middle chkFSite" onclick="chkFilterSiteItem();" <?php echo (in_array($site['siteid'], $arrSiteId)) ? 'checked="checked"' : '';?> /> <span class="ver-middle marleft5 fSiteName"><?php echo $site['sitename']; ?></span>
					</p>
				<?php
				}
				?>
			</div>
			
			<div id="divSlcFilterSite" style="height: 300px; overflow: auto; width: 450px; float: left; background: #fff;  padding: 10px;">
			</div>
		</div>	
		<br />
		<table cellpadding="0" cellspacing="0" border="0" class="">
			<tr>
				<td class="ver-middle padbottom10 padright10">
					<b>Theo ngày</b>
				</td>
				<td class="ver-middle padbottom10">
					<input type="text" value="<?php echo $fromdate . ' :: ' . $todate; ?>" id="date" name="date" class="input-time" style="width: 200px;"/>
				</td>
				
				<td class="ver-middle padbottom10 padleft10">
					<input type="button" value="      View      " onclick="viewZoneReport();" />
				</td>
			</tr>
		</table>
		
		<table cellpadding="0" cellspacing="0" border="0">
			<tr>
				<td class="ver-middle padbottom10 padright10">
					<b>Tìm kiếm</b>
				</td>
				<td class="ver-middle padbottom10 padright10">
					<input type="text" id="txtClientZone" style="padding: 4px 5px; width: 200px;" />
				</td>
				<td class="ver-middle padbottom10 padright10">
					<select id="slcSearchType">
						<option value="1">Tìm kiếm theo tên</option>
						<option value="2">Tìm kiếm kích thước</option>
					</select>
				</td>
				
				<td class="ver-middle padbottom10 padright10">
					<input type="text" id="txtClientZone2" style="padding: 4px 5px; width: 200px;" />
				</td>
				<td class="ver-middle padbottom10 padright10">
					<select id="slcSearchType2">
						<option value="1">Tìm kiếm theo tên</option>
						<option value="2">Tìm kiếm kích thước</option>
					</select>
				</td>
				<td class="ver-middle padbottom10 padright10" colspan="2">
					<input type="button" value="Search" onclick="searchZone();" />
				</td>
			</tr>
		</table>
		
		<input type="button" value="Export to .csv" onclick="zone_export_2csv();" />
		
		<table class="martop10" width="100%" cellpadding="0" cellspacing="0" border="0">
			<tr>
				<td>
					<b>Tổng số zones đang xem: </b> <b id="numViewZone" style="color: #ff6600; font-size: 14px;"><?php echo $numZone; ?></b>
					<span class="cursor-pointer marleft10" style="display: none;" id="viewAll" onclick="showAll();">(Xem tất cả)</span>
				</td>
				
				<td class="text-right">
					<input type="text" id="txtReportZone" style="width: 600px; padding: 3px 5px;" /> 
					<input type="button" value="View Report" onclick="zone_report('<?php echo $fromdate; ?>', '<?php echo $todate; ?>');" />
				</td>
			</tr>
		</table>
		
		<div style="width: 980px; overflow: auto;">
			<table id="tableData" class="martop10 tbl-list tablesorter" cellpadding="0" cellspacing="0" border="0">
				<thead>
					<tr>
						<th class="td-label text-center tblsort-header" width="25" style="background-image: none;">
							<input id="chkZoneAll" class="ver-middle" type="checkbox" title="Select All" onclick="chkReportAll(this, '.chkZid');" />
						</th>
						<th class="td-label text-center tblsort-header" width="60" style="background-image: none;">
							<b>ID</b>
						</th>
						
						<th class="td-label tblsort-header"  style="background-image: none;">
							<b>Tên</b>
						</th>
						
						<th class="td-label tblsort-header" width="90" style="background-image: none;">
							<b>Kích thước</b>
						</th>
						
						<th class="td-label tblsort-header"  style="background-image: none;">
							<b>Website</b>
						</th>
						
						<th class="td-label tblsort-header text-center" width="90" style="background-image: none;">
							<b>Total Click</b>
						</th>
						
						<th class="td-label tblsort-header text-center" width="90" style="background-image: none;">
							<b>Real Click</b>
						</th>
						
						<th class="td-label tblsort-header text-center" width="90" style="background-image: none;">
							<b>Click Charge</b>
						</th>
						
						<th class="td-label tblsort-header text-center" width="90" style="background-image: none;">
							<b>Total View</b>
						</th>
						
						<th class="td-label tblsort-header" width="70" style="background-image: none;">
							<b>CTR (%)</b>
						</th>
								
					</tr>
				</thead>
				<tbody id="tbodyListZone">
					<?php
					$index = 1;
					foreach($lstZone as $zone)
					{
						$odd = ($index%2 == 1) ? "" : "odd";
					?>
						<tr id="tr_<?php echo $zone['zoneid']; ?>" class="tr_zone <?php echo $odd; ?>">
							<td class="pad5 text-center" >
								<input type="checkbox" value="<?php echo $zone['zoneid']; ?>" class="chkZid" onclick="getZoneViewReport();" />
							</td>					
							<td class="pad5 text-center" >
								<?php echo $zone['zoneid']; ?>
							</td>
							
							<td class="pad5" >
								<?php echo $zone['zonename']; ?>
							</td>
							
							<td class="pad5 text-center" >
								<?php echo ($zone['width'] != '' && $zone['height'] != '') ? $zone['width'] . ' x ' . $zone['height'] : ''; ?>
							</td>
							
							<td class="pad5" >
								<?php
								if($zone['siteid'] > 0)
								{
								?>
									<a href="<?php echo site_url('zone/view_type') . "?s=" . $zone['siteid'] . "&fd=$fromdate&td=$todate" ?>">
										<?php echo $zone['sitename']; ?>
									</a>
									<?php echo ($zone['siteactive'] == 0 && $zone['sitename'] != '' ) ? '<img alt="Delete site" title="Delete site" src="/images/delete_icon.png" width="12" class="marleft5" />' : ''; ?>
								<?php
								}
								?>
							</td>
							
							<td class="pad5 text-right" >
								<?php echo ($zone['tc'] == '' || $zone['tc'] == -1) ? 'N/A' : number_format($zone['tc']); ?>
							</td>
							
							<td class="pad5 text-right" >
								<?php echo ($zone['rc'] == '' || $zone['rc'] == -1) ? 'N/A' : number_format($zone['rc']); ?>
							</td>
							
							<td class="pad5 text-right" >
								<?php echo ($zone['cc'] == '' || $zone['cc'] == -1) ? 'N/A' : number_format($zone['cc']); ?>
							</td>
							
							<td class="pad5 text-right" >
								<?php echo ($zone['v'] == '' || $zone['v'] == -1) ? 'N/A' : number_format($zone['v']); ?>
							</td>
							
							<td class="pad5 text-right" >
								<?php echo ($zone['ctr'] == -1 || $zone['ctr'] == '') ? 'N/A' : $zone['ctr']; ?>
							</td>
						</tr>
					<?php
						$index++;
					}
					?>
				</tbody>
				<tfoot>
					<tr id="trTotalAll">
						<td colspan="5" class="pad5 bold">
							Tổng
						</td>
						<td class="pad5 text-right bold">
							<span id="totalClick"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalRealClick"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalClickCharge"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalView"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalCtr"></span>
						</td>
					</tr>
					<tr id="trTotalAllFilter" style="display: none;">
						<td colspan="5" class="pad5 bold">
							Tổng
						</td>
						<td class="pad5 text-right bold">
							<span id="totalClickFilter"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalRealClickFilter"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalClickChargeFilter"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalViewFilter"></span>
						</td>
						<td class="pad5 text-right bold">
							<span id="totalCtrFilter"></span>
						</td>
					</tr>
				</tfoot>
			</table>
		</div>
		<br /><br /><br />
	</div>
	
</div>
<form id="frmExpCvs" action="<?php echo site_url('zone/view_type/?exp=cvs&fd=' . $fromdate . '&td=' . $todate) ?>" method="post" style="display: none;">
	<textarea id="txtexp" name="txtexp" ></textarea>
</form>
<img alt="" src="/images/ajax-loader.gif" id="imgLoadingPopup" style="display: none;" />
<div class="ajaxLoadingPopupBackground" id="ajaxLoadingPopupBackground"></div>
<script type="text/javascript">	
	function filterSizeAll(obj)
	{
		if($(obj).attr('checked'))
		{
			var strId = '';
			var arrSiteId = $('#divFilterSite .chkFSite');
			var numSite = arrSiteId.length;
			jQuery.each(arrSiteId, function(){
				$(this).attr('checked', true);
				strId += (strId == '') ? $(this).val() : ',' + $(this).val();
			});
			$('#hddFlterSite').val('');
			var arrSiteName = $('#divFilterSite .fSiteName');
			var str = '';
			jQuery.each(arrSiteName, function(){
		 		str += '<p class="padtop5">' + $(this).html() + '</p>';
			});
			$('#divSlcFilterSite').html(str);
			$('#viewNumSite').html(numSite + '/' + numSite);
			
			arrSiteId = null;
			arrSiteName = null;
		}
		else
		{
			var arrSiteId = $('#divFilterSite .chkFSite');
			var numSite = arrSiteId.length;
			$('#viewNumSite').html('0/' + numSite);
			$('#hddFlterSite').val('');
			$('#divSlcFilterSite').html('');
			$('#divFilterSite .chkFSite').attr('checked', false);
			arrSiteId = null;
		}
	}
	
	function getFilterSite()
	{
		var strId = '';
		var arrSiteId = $('#divFilterSite .chkFSite');
		var arrSiteName = $('#divFilterSite .fSiteName');
		var str = '';
		var num = arrSiteId.length;
		var numSelected = 0;
		jQuery.each(arrSiteId, function(index){
			if($(arrSiteId[index]).attr('checked'))
			{
				strId += (strId == '') ? $(arrSiteId[index]).val() : ',' + $(arrSiteId[index]).val();
				str += '<p class="padtop5">' + $(arrSiteName[index]).html() + '</p>';
				numSelected++;
			}
		});
		$('#viewNumSite').html(numSelected + '/' + num);
		if(numSelected == num)
		{
			$('#hddFlterSite').val('');
		}
		else
		{
			$('#hddFlterSite').val(strId);
			if(strId == '')
			{
				$('#viewNumSite').html(num + '/' + num);
			}
		}		
		$('#divSlcFilterSite').html(str);
	}
	
	function chkFilterSiteCheckAll()
	{
		var chk = true;
		var arrSiteId = $('#divFilterSite .chkFSite');
		var num = arrSiteId.length;
		for(var i=0; i<num; i++)
		{
			if(!$(arrSiteId[i]).attr('checked'))
			{
				chk = false;
				break;
			}
		}
		return chk;
	}
	
	function chkFilterSiteItem()
	{
		var isAll = chkFilterSiteCheckAll();
		$('#chkAllFilterSize').attr('checked', isAll);
		getFilterSite();
	}
	
	function viewZoneReport()
	{
		var arrTime = $('#date').val().split('::');
 		var fromdate = $.trim(arrTime[0]);
        var todate = $.trim(arrTime[1]);
        var siteId = $.trim($('#hddFlterSite').val());
		window.location = '/admin/zone/view_type?s=' + siteId + '&fd=' + fromdate + '&td=' + todate;
	}
	
	$(document).ready(function() {
		$('#slcQSiteFilter').val('all');
		$('input#date').DatePicker({
			format: 'd-m-Y',
			date: ['<?php echo $fromdate;?>','<?php echo $todate;?>'],
        	current: '<?php echo date('d-m-Y');?>',
			position: 'right',
			calendars: 2,
        	mode: 'range',
			onChange: function(formated, dates) 
			{
				$('#date').val(formated.join(' :: '));
				$('#date').DatePickerHide();				
			}
		});
		
		$.tablesorter.addWidget({ 
		    // give the widget a id 
		    id: "repeatHeaders", 
		    // format is called when the on init and when a sorting has finished 
		    format: function(table) { 
		        // cache and collect all TH headers 
		        if(!this.headers) { 
		            var h = this.headers = [];  
		            $("thead th",table).each(function() { 
		                h.push( 
		                    "" + $(this).text() + "" 
		                );
		            }); 
		        } 
		         
		        // remove appended headers by classname. 
		        $("tr.repated-header",table).remove(); 
		         
		        // loop all tr elements and insert a copy of the "headers"     
		        for(var i=0; i < table.tBodies[0].rows.length; i++) { 
		            // insert a copy of the table head every 10th row 
		            if((i%5) == 4) { 
		                $("tbody tr:eq(" + i + ")",table).before( 
		                    $("").html(this.headers.join("")) 
		                 
		                );     
		            } 
		        } 
		    } 
		}); 
	
		jQuery.tablesorter.addParser({
	      id: "fancyNumber",
	      is: function(s) {
	        return false;
	      },
	      format: function(s) {
	        return parseFloat(s.replace(/,/g,'')) ;
	      },
	      type: "numeric"
	    });
	    	    
	    <?php
	    if($cPage < $pCount)
	    {
    	?>
    		loadAjaxLoadingPopup('#imgLoadingPopup', '#ajaxLoadingPopupBackground');
			zone_ajax_view_type('<?php echo $strSiteId; ?>', '<?php echo $fromdate; ?>', '<?php echo $todate; ?>', '<?php echo $cPage + 1; ?>', '<?php echo $totalclick; ?>', '<?php echo $totalrealclick; ?>', '<?php echo $totalclickcharge; ?>', '<?php echo $totalview ?>');
    	<?php
	    }
		?>
	});
	
	function zone_ajax_view_type(strSiteId, fromdate, todate, cPage, totalclick, totalrealclick, totalclickcharge, totalview)
	{
		var _onSuccess = function(data)
		{
			var objData = jQuery.parseJSON(data);
			if(objData.login == 'login' || objData.right == 'notright')
			{
				window.location.reload(true);
				return;
			}
			else
			{
				if($.trim(objData.data) != '')
				{
					$('#tbodyListZone').append(objData.data);
					var currPage = parseInt(objData.cPage);
					var totalPage = parseInt(objData.pCount);
					var numItem = parseInt(objData.numZone);
					var currViewZone = parseInt($.trim($('#numViewZone').html()));
					$('#numViewZone').html(currViewZone + numItem);
					if(currPage < totalPage)
					{
						currPage = currPage + 1;
						zone_ajax_view_type(strSiteId, fromdate, todate, currPage, objData.totalclick, objData.totalrealclick, objData.totalclickcharge, objData.totalview);
					}
					else
					{
						$('#totalClick').html(objData.totalclick);
						$('#totalRealClick').html(objData.totalrealclick);
						$('#totalClickCharge').html(objData.totalclickcharge);
						$('#totalView').html(objData.totalview);
						$('#totalCtr').html(objData.totalctr);
						var myTextExtraction = function(node)
					    {  
					    	var cellIndex = node.cellIndex;
					    	var rel = '';
					    	switch(cellIndex)
					    	{
					    		case 0:
					    			break;
								case 1:
					                rel = $.trim($(node).html()); 
					    			break;
				 				case 2:
					                rel = $.trim($(node).html()); 
					    			break;
		    					case 3:
					                rel = $.trim($(node).html()); 
					    			break;
				 				case 4:
				                 	var arrA = $(node).find('a');
					                if(arrA.length > 0)
					                {
					                   rel = $.trim($(arrA[0]).html());
					                }
					    			break;
					    		case 5:
					    			rel = $.trim($(node).html());
					    			rel = rel.replace(/,/g,'');
					                rel = (rel == 'N/A' || rel == '' ) ? -1 : parseFloat(rel);
					    			break;
					    		case 6:
					    			rel = $.trim($(node).html());
					    			rel = rel.replace(/,/g,'');
					                rel = (rel == 'N/A' || rel == '') ? -1 : parseFloat(rel);
					    			break;
					    		case 7:
					    			rel = $.trim($(node).html());
					    			rel = rel.replace(/,/g,'');
					                rel = (rel == 'N/A' || rel == '') ? -1 : parseFloat(rel);
					    			break;
				    			case 8:
					    			rel = $.trim($(node).html());
					    			rel = rel.replace(/,/g,'');
					                rel = (rel == 'N/A' || rel == '') ? -1 : parseFloat(rel);
					    			break;
				    			case 9:
					    			rel = $.trim($(node).html());
					    			rel = rel.replace(/,/g,'');
					                rel = (rel == 'N/A' || rel == '') ? -1 : parseFloat(rel);
					    			break;
					    	}
					    	return rel;
					    };
				    	
						$('#tableData').tablesorter(
					    {
					    	textExtraction: myTextExtraction,
							widgets: ['zebra','repeatHeaders'],
					        headers: 
					        {
						        0: {sorter: false},
						        1: {sorter: 'numeric'},
						        2: {sorter: 'text'},
						        3: {sorter: 'text'},
						        4: {sorter: 'text'},
						        5: {sorter: 'numeric'},
						        6: {sorter: 'numeric'},
						        7: {sorter: 'numeric'},
						        8: {sorter: 'numeric'},
						        9: {sorter: 'numeric'}
					        }
					    });
					    alert('Hoàn tất load data');
					    disableAjaxLoadingPopup('#imgLoadingPopup', '#ajaxLoadingPopupBackground');
					}
				}
			}
		};
		var _onError = function()
		{
			disableAjaxLoadingPopup('#imgLoadingPopup', '#ajaxLoadingPopupBackground');
		};
		getAjax('zone_ajax_view_type', 's=' + strSiteId + '&fd=' + fromdate + '&td=' + todate + '&p=' + cPage + '&ttc=' + totalclick + '&ttrc=' + totalrealclick + '&ttcc=' + totalclickcharge + '&ttv=' + totalview, '', '', '', false, _onSuccess, _onError);
	}
	
	function chkReport(zId)
	{
		var currId = $.trim($('#txtReportZone').val());
		zId = $.trim(zId);
		if(zId != '')
		{
			var arr = currId.split(',');
			if(jQuery.inArray(zId, arr) == -1)
			{
				
				currId = (currId == '') ? zId : currId + ',' + zId;
				$('#txtReportZone').val(currId);
			}
		
		}
	}
	
	function zone_report(fromdate, todate)
	{
		var currId = $.trim($('#txtReportZone').val());
		if(currId == '')
		{
			alert('Bạn hãy chọn zone');
			return;
		}
		else
		{
			var arr = currId.split(',');
			window.location = '/admin/zone/zone_report?zid=' + currId + '&from=' + fromdate + '&to=' + todate;
		}
	}
	
	function zone_export_2csv()
	{
		var arrTr = $('#tableData tbody tr.tr_zone');
		var arrShow = new Array();
		var num = arrTr.length;
		
		for(var i = 0; i < num; i++)
		{
			if($.trim($(arrTr[i]).css('display')) != 'none')
			{
				arrShow.push(arrTr[i]);
			}
		}
		var listZone = new Object();
		var num = arrShow.length;
		var index = 0;
		if(num > 0)
		{
			for(var i = 0; i < num; i++)
			{
				var tmp = new Object();
				var arrTd = $(arrShow[i]).find('td');
				tmp['id'] = $.trim($(arrTd[1]).html());
				tmp['name'] = $.trim($(arrTd[2]).html());
				tmp['size'] = $.trim($(arrTd[3]).html());
				tmp['site'] = $.trim($(arrTd[4]).find('a').html());
				tmp['tc'] = $.trim($(arrTd[5]).html());
				tmp['rc'] = $.trim($(arrTd[6]).html());
				tmp['cc'] = $.trim($(arrTd[7]).html());
				tmp['v'] = $.trim($(arrTd[8]).html());
				tmp['ctr'] = $.trim($(arrTd[9]).html());
				
				arrTd = null;
				listZone[index] = tmp;
				tmp = null;
				index++;
			}
			var arrTrTotal = $('#tableData tfoot tr');
			if(arrTrTotal.length > 0)
			{
				for(var i=0; i < arrTrTotal.length; i++)
				{
					if($(arrTrTotal[i]).css('display') != 'none')
					{
						var tmp = new Object();
						var arrTd = $(arrTrTotal[i]).find('td');
						tmp['id'] = '';
						tmp['name'] = $.trim($(arrTd[0]).html());
						tmp['size'] = '';
						tmp['site'] = '';
						tmp['tc'] = $.trim($(arrTd[1]).find('span').html());
						tmp['rc'] = $.trim($(arrTd[2]).find('span').html());
						tmp['cc'] = $.trim($(arrTd[3]).find('span').html());
						tmp['v'] = $.trim($(arrTd[4]).find('span').html());
						tmp['ctr'] = $.trim($(arrTd[5]).find('span').html());
						listZone[index] = tmp;
						tmp = null;
					}
				}
			}
			$('#txtexp').val(JSON.stringify(listZone));
			listZone = null;
			$('#frmExpCvs').submit();
		}
		else
		{
			alert('Bạn chưa chọn zone nào để export.');
			return;
		}
	}
	
	function chkReportAll(obj, cssClass)
	{
		if($(obj).attr('checked'))
		{
			var arrTr = $('#tableData tbody tr.tr_zone');
			var arr = $('#tableData ' + cssClass);
			var numTr = arrTr.length;
			var strid = '';
			for(var i=0; i < numTr; i++)
			{
				var tmp = '';
				if($(arrTr[i]).css('display') != 'none')
				{
					var id = $.trim($(arr[i]).val());
					$(arr[i]).attr('checked', true);
					strid += (strid == '') ? id : ',' + id;
				}
			}
			$('#txtReportZone').val(strid);
		}
		else
		{
			$('#txtReportZone').val('');
			$('#tableData ' + cssClass).attr('checked', false);
		}
	}
	
	function getZoneViewReport()
	{
		var strId = '';
		var arr = $('#tableData .chkZid');
		var isAll = true;
		jQuery.each(arr, function(){
			if($(this).attr('checked'))
			{
				strId += (strId == '') ? $(this).val() : ',' + $(this).val();
			}
			else
			{
				isAll = false;
			}
		});
		$('#chkZoneAll').attr('checked', isAll);
		$('#txtReportZone').val(strId);
	}
	
	function searchZone()
	{
		var key = $.trim($('#txtClientZone').val());
		key = key.toLowerCase();
		var type = $.trim($('#slcSearchType').val());
		
		var keyFilter = $.trim($('#txtClientZone2').val());
		keyFilter = keyFilter.toLowerCase();
		var chkFilter = (keyFilter != '') ? true : false;
		
		var typeFilter = $.trim($('#slcSearchType2').val());
		if(key == '')
		{
			alert('Bạn hãy nhập từ khóa!');
			$('#txtClientZone').focus();
			return;
		}
		var arrTr = $('#tableData tbody#tbodyListZone tr.tr_zone');
		var arrHide = new Array();
		var arrShow = new Array();
		var numShow = 0;
		var numTr = arrTr.length;
		
		for(var i=0; i < numTr; i++)
		{
			var tmp = '';
			var tmpFilter = '';
			
			var arrTd = $(arrTr[i]).find('td');
			if(type== '1')
			{
				tmp = $.trim($(arrTd[2]).html());
			}
			else if(type == '2')
			{
				tmp = $.trim($(arrTd[3]).html());	
			}
			
			tmp = tmp.toLowerCase();
			var index = tmp.indexOf(key, 0);
			var indexFilter = -1;
			if(chkFilter != '')
			{
				if(typeFilter== '1')
				{
					tmpFilter = $.trim($(arrTd[2]).html());
				}
				else if(typeFilter == '2')
				{
					tmpFilter = $.trim($(arrTd[3]).html());	
				}
				
				tmpFilter = tmpFilter.toLowerCase();
				indexFilter = tmpFilter.indexOf(keyFilter, 0);
			}
			
			if(tmp != '')
			{
				if(index > -1)
				{
					if(chkFilter)
					{
						if(indexFilter > -1)
						{
							numShow++;
							arrShow.push(arrTr[i]);
						}
						else
						{
							arrHide.push(arrTr[i]);
						}
					}
					else
					{
						numShow++;
						arrShow.push(arrTr[i]);
					}
				}
				else
				{
					arrHide.push(arrTr[i]);
				}
			}
			else
			{
				arrHide.push(arrTr[i]);
			}
		}
		
		if(numShow > 0)
		{
			$('#numViewZone').html(numShow);
			$('#viewAll').show();
			
			jQuery.each(arrHide, function(){$(this).hide();});
			var tmpindex = 0;
			var totalClick = 0;
			var totalRealClick = 0;
			var totalClickCharge = 0;
			var totalView = 0;
			jQuery.each(arrShow, function(){
				$(this).removeClass('odd');
				var arrTd = $(this).find('td');
				var click = $.trim($(arrTd[5]).html());
				click = click.replace(/,/g,'');
   				click = (click == 'N/A' || click == '' ) ? 0 : parseFloat(click);
   				totalClick += click;
   				
   				var rclick = $.trim($(arrTd[6]).html());
				rclick = rclick.replace(/,/g,'');
   				rclick = (rclick == 'N/A' || rclick == '' ) ? 0 : parseFloat(rclick);
   				totalRealClick += rclick;
   				
   				var clickcharge = $.trim($(arrTd[7]).html());
				clickcharge = clickcharge.replace(/,/g,'');
   				clickcharge = (clickcharge == 'N/A' || clickcharge == '' ) ? 0 : parseFloat(clickcharge);
   				totalClickCharge += clickcharge;
   				
   				var view = $.trim($(arrTd[8]).html());
				view = view.replace(/,/g,'');
   				view = (view == 'N/A' || view == '' ) ? 0 : parseFloat(view);
   				totalView += view;
   				
				var cl = tmpindex%2 == 0 ? 'odd' : '';
				$(this).addClass(cl);
				$(this).show();
				tmpindex++;
			});
			
			if(totalView > 0)
			{
				var ctr = roundNumber((totalRealClick/totalView)*100,3);
				$('#totalCtrFilter').html(ctr);
			}
			else
			{
				$('#totalCtrFilter').html('N/A');
			}
			
			$('#totalClickFilter').html(addCommas(totalClick));
			$('#totalRealClickFilter').html(addCommas(totalRealClick));
			$('#totalClickChargeFilter').html(addCommas(totalClickCharge));
			$('#totalViewFilter').html(addCommas(totalView));
			$('#trTotalAll').hide();
			$('#trTotalAllFilter').show();
		}
	}
	
	function showAll()
	{
		var arr = $('#tableData tbody#tbodyListZone tr.tr_zone');
		$('#numViewZone').html(arr.length);
		$('#viewAll').hide();
		var tmpindex = 0;
		jQuery.each(arr, function(){
			$(this).removeClass('odd');
			var cl = tmpindex%2 == 0 ? 'odd' : '';
			$(this).addClass(cl);
			$(this).show();
			tmpindex++;
		});
		$('#trTotalAll').show();
		$('#trTotalAllFilter').hide();
		$('#txtClientZone').val('');
		$('#slcSearchType').val('1');
		$('#txtClientZone2').val('');
		$('#slcSearchType2').val('1');
	}
	
	function quickSiteSelect(obj)
	{
		var strId = $.trim($(obj).val());
		var arr = new Array();
		var arrId = new Array();
		if(strId != '' && strId != 'all')
		{
			arr = strId.split(',');
			arrId = new Array();
			jQuery.each(arr, function(index){
				if($.trim(arr[index]) != '')
				{
					arrId.push(arr[index]);
				}
			});
		}
		$('#divFilterSite .chkFSite').attr('checked', false);
		$('#chkAllFilterSize').attr('checked',false);
		jQuery.each(arrId, function(index){
			$('#chkSite_' + arrId[index]).attr('checked', true);
		});
		chkFilterSiteItem();
	}
	
	chkFilterSiteItem();
	
</script>